package com.example.hello;

import com.example.hello.client.Nav.BootstrapNavState;
import com.vaadin.shared.Connector;
import com.vaadin.ui.AbstractComponentContainer;
import com.vaadin.ui.Component;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class BootstrapNav extends AbstractComponentContainer {

    public enum Alignment {
        LEFT, NONE, RIGHT
    }

    @Override
    protected BootstrapNavState getState() {
        return (BootstrapNavState) super.getState();
    }

    public List<Component> getNavs() {
        List<Component> navs = new LinkedList<Component>();
        Iterator<Connector> itr = getState().getNavs().iterator();
        while (itr.hasNext()) {
            navs.add((Component) itr.next());
        }

        return navs;
    }

    public void setNavs(List<Component> navs) {
        List<Connector> connectorList = new LinkedList<Connector>();
        Iterator<Component> itr = navs.iterator();
        while (itr.hasNext()) {
            connectorList.add(itr.next());
        }

        getState().setNavs(connectorList);
    }

    public void addNav(Component nav) {
        addNav(nav, getNavs().size());
    }

    public void addNav(Component nav, int index) {
        if (nav.getParent() != null) {
            throw new IllegalArgumentException("Given nav already has a parent component");
        }

        getState().addNav(nav, index);
        super.addComponent(nav);

        markAsDirty();
    }

    public void replaceNav(Component oldNav, Component newNav) {
        if (!getNavs().contains(oldNav)) {
            throw new IllegalArgumentException("Given old nav does not exist in this group");
        }

        int oldIndex = getNavs().indexOf(oldNav);

        if (oldIndex == -1) {
            return;
        }

        removeNav(oldNav);
        addNav(newNav, oldIndex);
    }

    public void removeNav(Component nav) {
        if (!getNavs().contains(nav)) {
            return;
        }

        getState().removeNav(nav);
        super.removeComponent(nav);

        markAsDirty();
    }

    public void removeAllNavs() {
        getState().getNavs().clear();
        super.removeAllComponents();

        markAsDirty();
    }

    public Alignment getAlignment() {
        return Alignment.valueOf(getState().getAlignment());
    }

    public void setAlignment(Alignment alignment) {
        getState().setAlignment(alignment.toString());
    }

    @Override
    public void addComponent(Component c) {
        throw new UnsupportedOperationException("Use addNav method instead");
    }

    @Override
    public void removeComponent(Component c) {
        throw new UnsupportedOperationException("Use removeNav method instead");
    }

    @Override
    public void removeAllComponents() {
        throw new UnsupportedOperationException("Use removeAllNavs instead");
    }

    @Override
    public Iterator<Component> iterator() {
        return getNavs().iterator();
    }

    @Override
    public void replaceComponent(Component oldComponent, Component newComponent) {
        throw new UnsupportedOperationException("Use replaceNav method instead");
    }

    @Override
    public int getComponentCount() {
        return getNavs().size();
    }

}
